package com.zhuss.black.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.black.black.wechat.DelTopicBlackBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.black.db.dao.ext.BlackTopicDao;
import com.zhuss.util.JsonUtil;

/**
 * 删除评论黑名单 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("blackDelTopic")
public class BlackDelTopic extends AbstractProcessor<Object, DelTopicBlackBo> implements Processor<Object, DelTopicBlackBo> {

    @Autowired
	@Qualifier("blackRead")
	private BlackRead blackRead;
    
    @Autowired
	@Qualifier("blackTopicRead")
	private BlackTopicRead blackTopicRead;
    
    @Autowired
    private BlackTopicDao blackTopicDao;
    
    /**
     * 校验
     */
    @Override
    public void check(DelTopicBlackBo bo) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DelTopicBlackBo param) throws WpsException {
    	Long blackTopicId = param.getBlackTopicId();
    	List<Long> blackTopicIdList = param.getBlackTopicIdList();
    	blackTopicIdList = new ArrayList<Long>();
    	blackTopicIdList.add(blackTopicId);
    	param.setBlackTopicIdList(blackTopicIdList);
    	batchProcess(param);
    	param.setName(blackTopicId + "");
    	return null;
    }
    
    private void batchProcess(DelTopicBlackBo param) throws WpsException {
    	List<Long> blackTopicIdList = param.getBlackTopicIdList();
    	
    	if (blackTopicIdList == null || blackTopicIdList.isEmpty()) {
    		return;
    	}
    	
    	Integer deleted = param.getDeleted();
    	Long modifierId = param.getConcurrentUserId();
    	String modifier = param.getConcurrentUserName();
    	Date modifyTime = new Date();
    	
    	List<Long> blackTopicSonList = blackTopicRead.listSon(blackTopicIdList);
    	DelTopicBlackBo sonBo = new DelTopicBlackBo();
    	BeanUtils.copyProperties(param, sonBo);
    	if (blackTopicSonList != null && !blackTopicSonList.isEmpty()) {
    		sonBo.setBlackTopicIdList(blackTopicSonList);
    	} else {
    		sonBo.setBlackTopicIdList(null);
    	}
    	batchProcess(sonBo);
    	blackTopicDao.deleteByParentBatch(blackTopicIdList, deleted, modifierId, modifier, modifyTime);
    	blackTopicDao.deleteBatch(blackTopicIdList, deleted, modifierId, modifier, modifyTime);
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DelTopicBlackBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.黑名单.key());
    	record.setAction(LogActionEnum.黑名单删除评论.key());
    	record.setTargetId(param.getBlackTopicId());
    	record.setTargetName("评论内容：" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
